import {
    Viewer,
    Ellipsoid,
    GeographicProjection,
    Ion,
    Color as CesiumColor,
    PostProcessStageLibrary ,
    SingleTileImageryProvider,
    TileMapServiceImageryProvider,
    ArcGisMapServerImageryProvider,
    buildModuleUrl,
    ImageryLayer
} from  'cesium'

/**
 * 初始化加载天地图-cgcs2000坐标系
 * @param id
 */
const initMap=(id:string,isUnBaseLayer:boolean=false)=>{
    let cgs2000Ellipsolid = new Ellipsoid(6378137.0, 6378137.0, 6356752.31414035585)
    var cgs2000GeographicProj = new GeographicProjection(cgs2000Ellipsolid)
    Ion.defaultAccessToken =
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIzOTg2MTE1Zi05NWMwLTQzZjctYjhmNS04MzQ2NTFkOTQ2N2YiLCJpZCI6MzQwMjcsImlhdCI6MTcxMzk0NTk5NH0.QybcHRVWRYLhFN2AiQsZ0AQjsqH2uxIiKoymvKo7kkY'
    if(isUnBaseLayer){
        Ion.defaultServer="/";//取消cesium默认的地图服务
    }
    let viewer:any = new Viewer(id, {
        infoBox: false,
        geocoder: false,
        mapProjection: cgs2000GeographicProj,
        fullscreenButton: false,
        homeButton: false, //是否显示Home按钮
        sceneModePicker: false, //是否显示3D/2D选择器
        // baseLayerPicker: false,//是否显示图层选择器
        navigationHelpButton: false, //是否显示右上角的帮助按钮
        navigationInstructionsInitiallyVisible: false,
        animation: true, //是否创建动画小器件，左下角仪表
        timeline: true, //是否显示时间轴
        vrButton: false,
        selectionIndicator: false, //是否显示选取指示器组件
        requestRenderMode: false,  //开启显示渲染
        // maximumRenderTimeChange:Infinity, //开启显示渲染
        showRenderLoopErrors: true, //是否显示渲染错误
        //baseLayerPicker和imageryProvider同时存在避免api.cesium.com
        baseLayerPicker: false,
        orderIndependentTranslucency: false,
        maximumRenderTimeChange: Infinity,
        //解决图片打印问题
        contextOptions: {
            // glsl 使用webgl1 需要修改 参照：https://webgl2fundamentals.org/webgl/lessons/zh_cn/webgl1-to-webgl2.html
            // requestWebgl1: true,
            webgl: {
                alpha: true,
                depth: true,
                stencil: true,
                antialias: true,
                premultipliedAlpha: true,
                //通过canvas.toDataURL()实现截图需要将该项设置为true
                preserveDrawingBuffer: true,
                failIfMajorPerformanceCaveat: true
            }
        },
        // baseLayer: new ImageryLayer(new SingleTileImageryProvider({
        //     url: '',
        //     tileWidth:255,
        //     tileHeight:255
        // }))
    })
    if(isUnBaseLayer){
        viewer.imageryLayers.add( new ImageryLayer(new SingleTileImageryProvider({
            url: '',
            tileWidth:255,
            tileHeight:255
        })))
    }
    // let _this = this
    viewer._cesiumWidget._creditContainer.style.display = 'none' //隐藏版权信息
    //是否开启抗锯齿
    viewer.scene.postProcessStages.fxaa.enabled = false
    viewer.scene.screenSpaceCameraController.enableCollisionDetection = false
    const baseColor=CesiumColor.fromCssColorString('#000');
    viewer.scene.globe.baseColor = baseColor;
    viewer.scene.globe.depthTestAgainstTerrain = true;
    let stages = viewer.scene.postProcessStages;
    viewer.scene.brightness = viewer.scene.brightness || stages.add(PostProcessStageLibrary.createBrightnessStage());
    viewer.scene.brightness.enabled = true;
    viewer.scene.brightness.uniforms.brightness = Number(1.23);
    // 时间轴的timeline:true 和  animation:true,因为轨迹需要，使用下面方式隐藏组件
    // const timeLineContain:HTMLElement = <HTMLElement>document.querySelector(".cesium-viewer-timelineContainer");
    // if(timeLineContain){
    //     timeLineContain.style.display = 'none';
    // }
    // const animateContain:HTMLElement= <HTMLElement> document.querySelector(".cesium-viewer-animationContainer");
    // if(animateContain!==null){
    //     animateContain.style.display = 'none';
    // }
    return viewer;
}
export {
    initMap
}
